package com.spzx.product.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.spzx.common.core.utils.StringUtils;
import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.core.web.page.TableDataInfo;
import com.spzx.product.domain.ProductUnit;
import com.spzx.product.service.ProductUnitService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.spzx.common.core.web.controller.BaseController;

import java.util.List;

/**
 * <p>
 * 商品单位 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2024-10-26
 */
@Tag(name = "商品单位管理")
@RestController
@RequestMapping("/productUnit")
public class ProductUnitController extends BaseController {

    @Autowired
    private ProductUnitService productUnitService;

    @Operation(summary = "分页及带条件的查询")
    @GetMapping("/list")
    public TableDataInfo list(ProductUnit productUnit) {
        //开启分页
        startPage();
        //创建LambdaQueryWrapper对象
        LambdaQueryWrapper<ProductUnit> productUnitLambdaQueryWrapper = new LambdaQueryWrapper<>();
        //封装查询条件
        productUnitLambdaQueryWrapper.like(StringUtils.hasText(productUnit.getName()),ProductUnit::getName,productUnit.getName());
        //调用带条件查询的方法
        List<ProductUnit> productUnitList = productUnitService.list(productUnitLambdaQueryWrapper);
        return getDataTable(productUnitList);
    }

    @Operation(summary = "根据id查询商品规格")
    @GetMapping("/{id}")
    public AjaxResult getById(@PathVariable Long id){
        //调用ProductUnitService中根据id查询的方法
        ProductUnit productUnit = productUnitService.getById(id);
        return AjaxResult.success(productUnit);
    }

    @Operation(summary = "添加商品单位")
    @PostMapping
    public AjaxResult save(@RequestBody ProductUnit productUnit){
        //调用保存的方法
        boolean save = productUnitService.save(productUnit);
        return toAjax(save);
    }

    @Operation(summary = "根据id更新商品单位")
    @PutMapping
    public AjaxResult updateById(@RequestBody ProductUnit productUnit){
        //调用根据id更新的方法
        boolean updateById = productUnitService.updateById(productUnit);
        return toAjax(updateById);
    }

    @Operation(summary = "根据id删除和批量删除")
    @DeleteMapping("/{ids}")
    public AjaxResult removeById(@PathVariable List<Long> ids){
        //调用批量删除的方法
        boolean b = productUnitService.removeBatchByIds(ids);
        return toAjax(b);
    }

    @Operation(summary = "查询所有商品单位")
    @GetMapping("/getUnitAll")
    public AjaxResult getUnitAll(){
        //调用自带的查询所有的方法
        List<ProductUnit> productUnitList = productUnitService.list();
        return AjaxResult.success(productUnitList);
    }
}
